# -*- coding: utf-8 -*-
import scrapy
import re

class StocksSpider(scrapy.Spider):
    name = 'stocks'
    start_urls = ['http://quote.eastmoney.com/stocklist.html']

    def parse(self, response):
    	for href in response.css('a::attr(href)').extract():
    		try:
    			stockList = re.findall(r"[s][hz]\d{6}",href)
    			stock = ''
    			if not stockList == None and len(stockList)>0:
    				stock = stockList[0]
    			url = 'https://gupiao.baidu.com/stock/' + stock +'.html'
    			yield scrapy.Request(url,callback=self.parse_stock)
    		except Exception as e:
    			self.log("parse error",e)
    			continue

    def parse_stock(self,response):
    	infoDict = {}
    	stockInfo = response.css('.stock-bets')
    	name = stockInfo.css('.bets-name').extract()[0]
    	keyList = stockInfo.css('dt').extract()
    	valueList = stockInfo.css('dd').extract()
    	for i in range(len(keyList)):
    		key = re.findall(r">.*</dt>",keyList[i][0][1:-5])
    		try:
    			val = re.findall(r"\d+\.?.*</dd>",valueList[i][0][0:-5])[0]
    		except:
    			val = '--'
    		infoDict[key] = val
    	infoDict.update(
    		{'股票名称':re.findll('\s.*(',name)[0].split()[0] +
    		re.findall('\>.*\<',name)[0][1:-1]}
    		)
    	self.log(infoDict)
    	yield infoDict